package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.dao.ResStoreMapper;
import com.jingyanzi.resallocation.domain.StoreDO;
import com.jingyanzi.resallocation.entity.ResStore;
import com.jingyanzi.resallocation.entity.ResStoreExample;
import com.jingyanzi.resallocation.entity.ResStoreExample.Criteria;
import com.jingyanzi.resallocation.query.StoreQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.IStoreService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("storeService")
public class StoreServiceImpl extends BaseServiceImpl implements IStoreService {

	@Autowired
	private ResStoreMapper storeMapper;

	public ResultDO add(StoreDO storeDO) {
		ResStore store = new ResStore();

		ResultSupport result = BeanUtilsExtends.copy(store, storeDO);
		if (!result.isSuccess()){
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		store.setCreatetime(Calendar.getInstance().getTime());
		store.setIsDelete(1);
		int r = -1;

		try {
			r = storeMapper.insert(store);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加门店失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加门店失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}
	
	public ResultDO checkRepeat(StoreDO domain) {
		
		ResultSupport result = new ResultSupport();
		
		ResStoreExample example = new ResStoreExample();

		Criteria criteria = example.createCriteria();
		
		if(isNumberNotEmpty(domain.getId())) {
			criteria.andIdNotEqualTo(domain.getId());
			if(StringUtils.isNotEmpty(domain.getName())) {
				criteria.andNameEqualTo(domain.getName());
			}
			if(StringUtils.isNotEmpty(domain.getCode())) {
				criteria.andCodeEqualTo(domain.getCode());
			}
		} else {
			if(StringUtils.isNotEmpty(domain.getName())) {
				criteria.andNameEqualTo(domain.getName());
			}
			if(StringUtils.isNotEmpty(domain.getCode())) {
				criteria.andCodeEqualTo(domain.getCode());
			}
		}
		
		if(StringUtils.isNotEmpty(domain.getCode()) ||  StringUtils.isNotEmpty(domain.getName())) {
			int count = 0;
			
			try {
				count = storeMapper.countByExample(example);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "查询门店失败");
				result.setSuccess(false);
				return result;
			}
			
			if(count > 0) {
				result.setModel(ResultDO.FIRST_MODEL_KEY, count);
				return result;
			}
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, 0);
		
		return result;
	}

	public ResultDO edit(StoreDO storeDO) {
		ResStore store = new ResStore();

		ResultSupport result = BeanUtilsExtends.copy(store, storeDO);
		if (!result.isSuccess()){
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = storeMapper.updateByPrimaryKeySelective(store);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改门店失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改门店失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO del(Integer[] id) {
		ResStore store = new ResStore();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < id.length; i++) {
			store.setId(id[i]);
			store.setIsDelete(-1);
			int r = -1;

			try {
				r = storeMapper.updateByPrimaryKeySelective(store);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除门店失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除门店失败");
				result.setSuccess(false);
				return result;
			}
		}
		
		return result;
	}

	public ResultDO get(Integer id) {
		ResStore store = new ResStore();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			store = storeMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取门店失败");
			result.setSuccess(false);
			return result;
		}

		if (store == null) {
			result.setErrorCode(ResultDO.STORE_GET_ERROR);
			result.setErrorMsg("不存在该门店");
			result.setSuccess(false);
			return result;
		}
		StoreDO storeDO = getStoreDO(store);
		result.setModel(ResultDO.FIRST_MODEL_KEY, storeDO);
		return result;
	}

	public ResultDO query(StoreQueryDO storeQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResStore> list = null;

		ResStoreExample example = new ResStoreExample();

		Criteria createCriteria = example.createCriteria();
		if (storeQueryDO.getType() != null && storeQueryDO.getType() != 0) {
			createCriteria.andTypeEqualTo(storeQueryDO.getType());
		}
		
		if (StringUtils.isNotEmpty(storeQueryDO.getName())) {
			createCriteria.andNameLike("%"+storeQueryDO.getName()+"%");
		}
		
		if(StringUtils.isNotEmpty(storeQueryDO.getProvince())) {
			createCriteria.andProvinceEqualTo(storeQueryDO.getProvince());
		}
		
		if(StringUtils.isNotEmpty(storeQueryDO.getCity())) {
			createCriteria.andProvinceEqualTo(storeQueryDO.getCity());
		}
		
		if(StringUtils.isNotEmpty(storeQueryDO.getDistrict())) {
			createCriteria.andProvinceEqualTo(storeQueryDO.getDistrict());
		}
		
		createCriteria.andIsDeleteEqualTo(1);
		List<StoreDO> storeDOs = null;

		if (storeQueryDO.isPage()) {
			example.setLimitByPage(storeQueryDO.getStart() + "," + storeQueryDO.getPageRows());
			long count = storeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = storeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取门店失败");
			result.setSuccess(false);
			return result;
		}

		storeDOs = getStoreDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, storeDOs);
		return result;
	}

}
